package com.copm.demonstrationsystem.service.introductionfeatures.impl;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.commons.util.ImageUtils;
import com.copm.demonstrationsystem.config.SystemConfig;
import com.copm.demonstrationsystem.dao.commons.ImageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.ProjectIntroductionDao;
import com.copm.demonstrationsystem.pojo.commons.Image;
import com.copm.demonstrationsystem.pojo.introductionfeatures.ProjectIntroduction;
import com.copm.demonstrationsystem.service.introductionfeatures.ProjectIntroductionService;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.SinglonImageHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.TransformImagePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.resource.TransformedResource;

import java.io.FileNotFoundException;

/**
 * @Author: Yuan Baiyu
 * @Date: Created in 14:52 2018/4/22
 */
@Service
public class ProjectIntroductionServiceImpl implements ProjectIntroductionService {

    private static Logger logger = LoggerFactory.getLogger(ProjectIntroductionServiceImpl.class);

    @Autowired
    private SystemConfig config;

    @Autowired
    ProjectIntroductionDao projectIntroductionDao;

    @Autowired
    ImageDao imageDao;

    @Autowired
    SinglonImageHandle singlonImageHandle;

    @Autowired
    TransformImagePath transformImagePath;

    /**
     * 获取“项目介绍”页面信息
     *
     * @return
     */
    @Override
    public ResponseStatus getProjectIntroductionPage() {
        ResponseStatus status = new ResponseStatus();
        //查找页面
        ProjectIntroduction projectIntroduction = projectIntroductionDao.findPage();
        if (null == projectIntroduction) {
            status.setStatue(0);
            status.setMessage("项目介绍 页面查询为空");
            return status;
        }
        Image image = projectIntroduction.getImage();
        if (null == image) {
            projectIntroduction.setImage(null);
        } else {
            transformImagePath.transformImagePath(image);
        }
        //设置响应值
        status.setStatue(1);
        status.setData(projectIntroduction);
        logger.info("项目介绍 页面查询成功！");
        return status;
    }

    /**
     * 修改“项目介绍”页面文本
     *
     * @param content
     * @return
     */
    @Override
    public ResponseStatus modifyProjectIntroductionText(String content) {
        ResponseStatus status = new ResponseStatus();
        //查询“项目介绍”页面（只有一个页面数据）
        ProjectIntroduction projectIntroduction = projectIntroductionDao.findPage();
        if (null == projectIntroduction) {
            //新建“项目介绍”页面对象
            projectIntroduction = new ProjectIntroduction();
        }
        //设置页面对象
        projectIntroduction.setIntroduction(content);
        //保存页面对象
        projectIntroductionDao.save(projectIntroduction);
        status.setStatue(1);
        logger.info("修改“项目介绍”页面成功！");
        return status;
    }

    /**
     * 添加“项目介绍”页面图片
     *
     * @param file
     * @return
     */
    @Override
    public ResponseStatus addProjectIntroductionImage(MultipartFile file) {
        ResponseStatus status = singlonImageHandle.storeAndCheckImage(file);
        if (status.getStatue() == 0) {
            return status;
        }
        //获取图片存储信息
        String[] paths = (String[]) status.getData();
        Image image = new Image(paths[0], paths[1], paths[2]);
        //获取“项目介绍”页面对象
        ProjectIntroduction projectIntroduction = projectIntroductionDao.findPage();
         //设置“项目介绍”页面图片
        projectIntroduction.setImage(image);
        //保存添加了图片的对象
        projectIntroductionDao.save(projectIntroduction);
        status.setStatue(1);
        logger.info(" 项目介绍 页面图片添加成功！ ");
        return status;
    }

    /**
     * 删除“项目介绍”页面图片
     *
     * @return
     */
    @Override
    public ResponseStatus deleteProjectIntroductionImage() {
        ResponseStatus status;
        //获取页面对象
        ProjectIntroduction projectIntroduction = projectIntroductionDao.findPage();
        //获取图片名
        Image image = projectIntroduction.getImage();
        status = singlonImageHandle.deleteImage(image);
        if (status.getStatue() == 0) {
            return status;
        }
        //临时保存图片ID
        Long tempId = image.getId();
        //获取“项目介绍”页面 ，设置图片为空
        projectIntroduction.setImage(null);
        //保存已删除的页面对象
        projectIntroductionDao.save(projectIntroduction);
        //从图片对象中删除页面所对应的图片数据
        imageDao.delete(tempId);

        status.setStatue(1);
        logger.info(" 项目介绍 页面图片删除成功！ ");
        return status;
    }


    /**
     * 修改“项目介绍”页面图片
     *
     * @param file
     * @return
     */
    @Override
    public ResponseStatus modifyProjectIntroductionImage(MultipartFile file) {
        ResponseStatus status;
        //查找页面
        ProjectIntroduction projectIntroduction = projectIntroductionDao.findPage();
        //对象判空，即代表此页面对象不存在，需要先创建
        if (null == projectIntroduction) {
            projectIntroduction = new ProjectIntroduction();
            //保存对象
            projectIntroductionDao.save(projectIntroduction);
        }
        if (null != projectIntroduction.getImage()) {
            status = deleteProjectIntroductionImage();
            if (status.getStatue() == 0) {
                return status;
            }
        }
        //添加图片
        status = addProjectIntroductionImage(file);
        if (1 == status.getStatue()) {
            logger.info(" 项目介绍页面图片修改成功！ ");
        }
        return status;
    }
}
